--施法技能
--                                 施法
--                    ----------------------------
--                    |                          |
-- 技能起手         开始引导   间隔触发         引导结束           技能结束
--   |                |      | | |               |               |
--   -------------------------------------------------------------
--         前摇阶段        引导持续阶段                    后摇阶段
---@class Battle.SkillBase:Battle.BaseComponent
---@field New fun():Battle.SkillBase
local SkillBase = Battle.CreateClass("SkillBase",Battle.BaseComponent)
Battle.SkillBase = SkillBase
--技能创建
function SkillBase:OnCreate(actor,skillId)
    ---@type Battle.Entity
    self.actor = actor
    self.skillId = skillId
    self.isStart = false    --技能开始
    self.isEnd = false      --技能结束
    self.priority = 0   --技能优先级
    self.frontSwingTime = 0 --前揺时长
    self.afterSwingTime = 0  --后摇时长
    self.speedRate = 1  --技能加速
    --cd
    self.playingTime = 0;--技能播放总时间
    self.isInCd = false
    self.initCd = 0--初始cd
    self.maxCd = 0  --技能cd
    self.curCd = 0
    self.curEndCd = 0
    self.during = 0--技能时长
    self.duringTime = 0
end
function SkillBase:OnDestroy()
    self.actor = nil
end
function SkillBase:Update(dt)
    if self.isInCd then
        self.curCd = self.curCd - dt
        if self.curCd <= 0 then
            self:CoolDownFinished()
        end
    end
    if self.isStart then
        self.duringTime = self.duringTime - dt
        if self.duringTime <= 0 then
            self:End()
        end
        for k, v in pairs(self.components) do
            if v:IsUpdate() then
                v:Update(dt)
            end
        end
    end
end
---CD倒计时
function SkillBase:StartCoolDown(time)
    self.curCd = time
    self.isInCd = true
    self.actor.eventSystem:FireEvent(Battle.Event.SkillCDChange,Battle.EventArgs.SkillCdChangeArgs:Create(self,time,time))
end
---CD倒计时结束
function SkillBase:CoolDownFinished()
    self.isInCd = false
    self.curCd = 0
    self.actor.eventSystem:FireEvent(Battle.Event.SkillCDChange,Battle.EventArgs.SkillCdChangeArgs:Create(self,0,self.maxCd))
end

---前揺结束
function SkillBase:FrontSwingFinished()
    self.actor.eventSystem:FireEvent(Battle.Event.SkillFrontSwingFinished,Battle.EventArgs.SkillArgs:Create(self))
end
---技能结束
function SkillBase:OnSkillEnd()
    self.actor.eventSystem:FireEvent(Battle.Event.SkillEnd,Battle.EventArgs.SkillArgs:Create(self))
end

function SkillBase:IsInCD()
    return self.isInCd
end
function SkillBase:IsSkillEnd()
    return self.isEnd
end
function SkillBase:IsStart()
    return self.isStart
end
---技能初始化
function SkillBase:OnSkillInit()
end
---技能起手(前揺)
function SkillBase:OnSkillStart()
    self.isEnd = false
    self.isStart = true
    self.duringTime = self.during * self.speedRate
    if self.maxCd > 0 then
        self:StartCoolDown(self.maxCd)
    end
end
function SkillBase:End(isInterrupt)
    self:OnSkillEnd()
end
---技能结束
function SkillBase:OnSkillEnd()
    self.isEnd = true
    self.isStart = false
    for k, v in pairs(self.components) do
        if v.SkillEnd then
            v:SkillEnd()
        end
    end
end
